package ssh;

import filesystem.CFile;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class SSHUtils {

public static boolean RegisterUser(String user, String pass)
{
	try{
	 JSch jsch=new JSch();
	 Session session=jsch.getSession(SSHConfig.SSH_USER, SSHConfig.SSH_URL, SSHConfig.SSH_PORT);
     
	 UserInfo ui = new MyUserInfo(SSHConfig.SSH_PASS);
	 session.setUserInfo(ui);
     //session.connect();
     session.connect(30000);   // making a connection with timeout.
     
     PipedInputStream in = new PipedInputStream();
     PipedOutputStream out = new PipedOutputStream(in);
     
     Channel channel=session.openChannel("shell");

     channel.setInputStream(in);
     channel.setOutputStream(null);
     channel.connect(3*1000);
     String u = "useradd -m "+user+"\n";
     out.write(u.getBytes());
     out.flush();
     u = "passwd "+user+"\n";
     out.write(u.getBytes());
     out.flush();
     Thread.sleep(1000);
     u = pass+"\n";
     out.write(u.getBytes());
     out.flush();
     Thread.sleep(1000);
     u = pass+"\n";
     out.write(u.getBytes());
     out.flush();
     Thread.sleep(1000);
     /*Thread.sleep(1000);
     u = "rm /home/"+user+"/examples.desktop\n";
     out.write(u.getBytes());
     u = "chmod 700 /home/"+user;
     out.write(u.getBytes());*/
     return true;
   }
   catch(Exception e){
     e.printStackTrace();
     return false;
   }
}

public static void buildFileSystem(String user, String pass, String dir, objects.user.UserInfo userInfo, long parent) {
	try {
		StringBuilder sb = new StringBuilder();
		DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dfm.setTimeZone(TimeZone.getTimeZone("Europe/London"));
		String cmd = "ls -l "
				+ dir
				+ " |  sed \"1 d\" | awk '{print(substr($1,0,1));print($5);print($6);print($7);print($8)}'";
		JSch jsch=new JSch();
		Session session=jsch.getSession(user, SSHConfig.SSH_URL, SSHConfig.SSH_PORT);
		UserInfo ui = new MyUserInfo(pass);
		session.setUserInfo(ui);
		Channel channel = session.openChannel("exec");
		((ChannelExec) channel).setCommand(cmd);

		channel.setInputStream(null);
		((ChannelExec) channel).setErrStream(System.err);

		InputStream in = channel.getInputStream();

		channel.connect();

		byte[] tmp = new byte[1024];
		while (true) {
			while (in.available() > 0) {
				int i = in.read(tmp, 0, 1024);
				if (i < 0)
					break;
				// System.out.print(new String(tmp, 0, i));
				sb.append(new String(tmp, 0, i));
			}
			if (channel.isClosed()) {
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (Exception ee) {
			}
		}
		channel.disconnect();

		BufferedReader reader = new BufferedReader(new StringReader(
				sb.toString()));
		String line;
		String data = "";
		int count = 0;
		
		while ((line = reader.readLine()) != null) {
			data += line + "|";
			count++;
			if (count == 5) {
				String[] tok = data.split("\\|");
				String date = tok[2] + " " + tok[3] + ":00";
				CFile file  = new CFile(tok[4], tok[0].equals("d")? "0MB":tok[1]+"MB", date, tok[0].equals("d"));
				userInfo.fs.addFile(parent,file);
				count = 0;
				data = "";
			}
		}

	} catch (Exception e) {
		e.printStackTrace();
	}

}

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
	
	public MyUserInfo(String pass) {
		this.passwd = pass;
	}
	
	public String getPassword() {
		return passwd;
	}

	public boolean promptYesNo(String str) {
		return true;
	}

	String passwd;
	
	public void setPassword(String passwd) {
		this.passwd = passwd;
	}
	
	public String getPassphrase() {
		return null;
	}

	public boolean promptPassphrase(String message) {
		return true;
	}

	public boolean promptPassword(String message) {
		return true;
	}

	public void showMessage(String message) {
	}

	public String[] promptKeyboardInteractive(String destination,
			String name, String instruction, String[] prompt, boolean[] echo) {
		return null;
	}
}
}